package com.jx.lcs.messagepush.service;

import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tianyu.jty.common.persistence.HibernateDao;
import com.tianyu.jty.common.persistence.Page;
import com.tianyu.jty.common.service.BaseService;
import com.jx.lcs.messagepush.dao.MessagePushDao;
import com.jx.lcs.messagepush.entity.SysMessage;

/**
 * service
 * @author ty
 * @date 2015年1月13日
 */
@Service
@Transactional(readOnly = true)
public class MessagePushService extends BaseService<SysMessage, String> {

	@Autowired
	private MessagePushDao messagePushDao;

	@Override
	public HibernateDao<SysMessage, String> getEntityDao() {
		return messagePushDao;
	}
	public Page<SysMessage> findSysMessageList(Page<SysMessage> page, HttpServletRequest request) throws Exception {
		String name = request.getParameter("name");
		String keyword = request.getParameter("keyword");
		
		StringBuilder sb = new StringBuilder();
		sb.append("from SysMessage t where 1 = 1");
		
		if(name != null && !"".equals(name.trim())) {
			sb.append(" and t.sendUid like '%").append(name).append("%'");
		}
		if(keyword != null && !"".equals(keyword.trim())) {
			keyword = URLDecoder.decode(keyword, "UTF-8");
			sb.append(" and t.content like '%").append(keyword).append("%'");
		}
		
		sb.append(" order by t.sendtime desc");
		
		return messagePushDao.findPage(page, sb.toString(), new Object[]{});
	}
}
